package com.luomo.wenyuan.modules.wenyuan.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.luomo.wenyuan.common.utils.PageUtils;
import com.luomo.wenyuan.common.utils.R;
import com.luomo.wenyuan.modules.wenyuan.dao.TBookDetailDao;
import com.luomo.wenyuan.modules.wenyuan.entity.TBookDetailEntity;
import com.luomo.wenyuan.modules.wenyuan.entity.TBookEntity;
import com.luomo.wenyuan.modules.wenyuan.entity.TBorrowEntity;
import com.luomo.wenyuan.modules.wenyuan.service.TBookDetailService;
import com.luomo.wenyuan.modules.wenyuan.service.TBookService;
import com.luomo.wenyuan.modules.wenyuan.service.TBorrowService;
import com.luomo.wenyuan.modules.wenyuan.vo.TBorrowBookVo;
import com.luomo.wenyuan.modules.wenyuan.vo.TBorrowShowVo;
import com.luomo.wenyuan.modules.wenyuan.vo.TBorrowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * @author
 * @email
 * @date 2020-10-19 15:58:39
 */
@RestController
@RequestMapping("generator/tborrow")
public class TBorrowController {
    @Autowired
    private TBorrowService tBorrowService;

    @Autowired
    private TBookDetailDao tBookDetailDao;

    @Autowired
    private TBookDetailService tBookDetailService;

    @Autowired
    private TBookService tBookService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tBorrowService.queryPage(params);
        List<TBorrowEntity> list = (List<TBorrowEntity>) page.getList();
        if(CollectionUtil.isNotEmpty(list)){
            List<TBorrowShowVo> tBorrowShowVos = new ArrayList<>();
            TBorrowShowVo showVo;
            for (TBorrowEntity tBorrowEntity : list) {
                showVo = new TBorrowShowVo();
                BeanUtil.copyProperties(tBorrowEntity,showVo);
                Integer bookDetailId = tBorrowEntity.getBookDetailId();
                Integer bookId = tBookDetailService.getById(bookDetailId).getBookId();
                TBookEntity bookEntity = tBookService.getById(bookId);
                showVo.setBookId(bookId);
                showVo.setBookName(bookEntity.getName());
                showVo.setType(bookEntity.getType());
                showVo.setAuthor(bookEntity.getAuthor());
                showVo.setImg(bookEntity.getImg());
                showVo.setIsbn(bookEntity.getIsbn());
                tBorrowShowVos.add(showVo);
            }
            page.setList(tBorrowShowVos);
        }
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TBorrowEntity tBorrow = tBorrowService.getById(id);

        return R.ok().put("tBorrow", tBorrow);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody TBorrowVo tBorrowVo) {
        List<TBorrowBookVo> bookList = tBorrowVo.getBookList();
        String readerId = tBorrowVo.getReaderId();
        List<TBorrowEntity> tBorrowEntities = new ArrayList<>();
        List<TBookDetailEntity> tBookDetailEntities = new ArrayList<>();
        Date now = new Date();
        Date expiredDate = DateUtil.offset(now, DateField.DAY_OF_MONTH,30);
        TBorrowEntity tBorrowEntity;
        for (TBorrowBookVo book : bookList) {
            TBookDetailEntity bookDetail = tBookDetailDao.getOneCanBorrow(book.getBookId());
            Integer bookDetailId = bookDetail.getId();
            tBorrowEntity = new TBorrowEntity();
            tBorrowEntity.setBookDetailId(bookDetailId);
            tBorrowEntity.setReaderId(readerId);
            tBorrowEntity.setStatus(1);
            tBorrowEntity.setBorrowDate(now);
            tBorrowEntity.setExpireDate(expiredDate);
            tBorrowEntity.setNote(book.getNote());
            tBorrowEntities.add(tBorrowEntity);
            bookDetail.setStatus(4);
            tBookDetailEntities.add(bookDetail);
        }
        tBorrowService.saveBatch(tBorrowEntities);
        tBookDetailService.updateBatchById(tBookDetailEntities);
        return R.ok();
    }

    /**
     * 续借
     */
    @RequestMapping("/renew")
    @Transactional(rollbackFor = Exception.class)
    public R renew(@RequestBody List<TBorrowShowVo> tBorrowShowVos) {
        List<TBorrowEntity> tBorrowEntities = new ArrayList<>();
        for (TBorrowShowVo tBorrowShowVo : tBorrowShowVos) {
            Integer id = tBorrowShowVo.getId();
            TBorrowEntity borrowEntity = tBorrowService.getById(id);
            Date expireDate = borrowEntity.getExpireDate();
            DateTime newExpireDate = DateUtil.offset(expireDate, DateField.DAY_OF_MONTH, 30);
            borrowEntity.setExpireDate(newExpireDate);
            tBorrowEntities.add(borrowEntity);
        }
        tBorrowService.updateBatchById(tBorrowEntities);
        return R.ok();
    }

    /**
     * 归还
     * @param tBorrowShowVos
     * @return
     */
    @RequestMapping("/back")
    @Transactional(rollbackFor = Exception.class)
    public R back(@RequestBody List<TBorrowShowVo> tBorrowShowVos) {
        List<TBorrowEntity> tBorrowEntities = new ArrayList<>();
        List<TBookDetailEntity> tBookDetailEntities = new ArrayList<>();
        Date now = new Date();
        for (TBorrowShowVo tBorrowShowVo : tBorrowShowVos) {
            Integer id = tBorrowShowVo.getId();
            TBorrowEntity borrowEntity = tBorrowService.getById(id);
            borrowEntity.setStatus(2);
            borrowEntity.setBackDate(now);
            tBorrowEntities.add(borrowEntity);
            Integer bookDetailId = tBorrowShowVo.getBookDetailId();
            TBookDetailEntity bookDetailEntity = tBookDetailService.getById(bookDetailId);
            bookDetailEntity.setStatus(1);
            bookDetailEntity.setUpDate(now);
            tBookDetailEntities.add(bookDetailEntity);
        }
        tBorrowService.updateBatchById(tBorrowEntities);
        tBookDetailService.updateBatchById(tBookDetailEntities);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TBorrowEntity tBorrow) {
        tBorrowService.updateById(tBorrow);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        tBorrowService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
